/**
 * Created by lwx on 2018/3/22.
 */
$(function () {
    drawPic();
});
//登录验证码
var code = "";
//手机号正则表达式
var mobileReg = /^1[34578]\d{9}$/;
//登录
function login() {
    var phone = $("#loginName").val();
    var password = $("#loginPwd").val();
    if($("#loginName").val().length <= 0){
        showLoginError("请输入正确的用户名");
        $("#loginName").focus();
        return;
    }
    if($("#loginPwd").val().length < 6){
        showLoginError("密码不能少于6位");
        $("#loginPwd").focus();
        return;
    }
    if($("#loginCode").val().toUpperCase() != code){
        showLoginError("验证码不正确");
        $("#loginCode").focus();
        return;
    }
    if(!$("#loginCheck").is(":checked")){
        showLoginError("没有同意服务协议");
        return;
    }
    $.ajax({
        type:"get",
        url:"/user/login",
        data:{loginPhone:phone,loginPwd:password},
        dataType:"json",
        success:function (result) {
            if(result.loginResult == "success"){
                location.reload();
            }else{
                showLoginError("用户名或密码错误");
                console.log("登录失败");
            }
        },
        error:function(error){
            console.log(error);
        }
    });
}
var s = 0;
var n = 60;
//注册
function register() {
    if (!mobileReg.test($("#resPhone").val()) || $("#resPhone").val().length <= 0) {
        showRegisterError("手机号格式不正确");
        $("#resPhone").focus();
        return;
    }
    if ($("#resPwd").val().length < 6) {
        showRegisterError("密码长度不能少于6位");
        $("#resPwd").focus();
        return;
    }
    if ($("#resRePwd").val() != $("#resPwd").val()) {
        showRegisterError("俩次输入的密码不一致");
        $("#resRePwd").focus();
        return;
    }
    if (!$("#resCheck").is(":checked")) {
        showRegisterError("没有同意服务协议");
        return;
    }
    if ($("#resReCode").val().length <= 0) {
        showRegisterError("验证码不能为空");
        $("#resReCode").focus();
        return;
    }
    var phone = $("#resPhone").val();
    var password = $("#resPwd").val();
    var resCode = $("#resCode").val();

    $.ajax({
        url: "/user/register",
        type: "get",
        data: {phone: phone, password: password, code: resCode},
        dataType: "json",
        success: function (result) {
            if (result.resResult == "success") {
                location.reload();
            } else {
                showRegisterError("验证码不正确");
                $("#resReCode").focus();
            }
        }
    });
}
//退出登录
function loginOut() {
    $.get("/user/loginOut",function (result) {
       location.reload();
    });
}

//获取验证码
function sendMsg() {
    if(!mobileReg.test($("#resPhone").val()) || $("#resPhone").val().length <= 0){
        showRegisterError("手机号格式不正确");
        $("#resPhone").focus();
    }else{
        $.get("/user/sendMsg",{phone:$("#resPhone").val()},function (result) {
            if(result == "1"){
                //禁用发送按钮
                $("#resReCode").attr("disabled","true");
                //启动定时器
                s = setInterval(function () {
                    jishi();
                },1000);
            }
        });
    }
}
//计时
function jishi() {
    n -= 1;
    if(n == 0){
        $("#resReCode").val("重新获取");
        $("#resReCode").removeAttr("disabled");
        n = 60;
        //停止计时
        clearInterval(s);
    }else{
        $("#resReCode").val(n + "s后,重新发送");
    }

}
//ajax获取登录验证码
function getCode() {
    $.get("/user/getCode",function (result) {
        alert(result);
        drawPic(result);
    });
}
/**生成一个随机数**/
function randomNum(min,max){
    return Math.floor( Math.random()*(max-min)+min);
}
/**生成一个随机色**/
function randomColor(min,max){
    var r = randomNum(min,max);
    var g = randomNum(min,max);
    var b = randomNum(min,max);
    return "rgb("+r+","+g+","+b+")";
}
/**绘制验证码图片**/
function drawPic(){
    code = "";
    var canvas=document.getElementById("canvas");
    var width=canvas.width;
    var height=canvas.height;
    var ctx = canvas.getContext('2d');
    ctx.textBaseline = 'bottom';

    /**绘制背景色**/
    ctx.fillStyle = randomColor(180,240); //颜色若太深可能导致看不清
    ctx.fillRect(0,0,width,height);
    /**绘制文字**/
    var str = 'ABCEFGHJKLMNPQRSTWXY123456789';
    for(var i=0; i<4; i++){
        var txt = str[randomNum(0,str.length)];
        code += txt;
        ctx.fillStyle = randomColor(50,160);  //随机生成字体颜色
        ctx.font = 30 + 'px SimHei'; //随机生成字体大小
        var x = 10+i*25;
        var y = randomNum(25,45);
        var deg = randomNum(-45, 45);
        //修改坐标原点和旋转角度
        ctx.translate(x,y);
        ctx.rotate(deg*Math.PI/180);
        ctx.fillText(txt, 0,0);
        //恢复坐标原点和旋转角度
        ctx.rotate(-deg*Math.PI/180);
        ctx.translate(-x,-y);
    }
    /**绘制干扰线**/
    for(var i=0; i<8; i++){
        ctx.strokeStyle = randomColor(40,180);
        ctx.beginPath();
        ctx.moveTo( randomNum(0,width), randomNum(0,height) );
        ctx.lineTo( randomNum(0,width), randomNum(0,height) );
        ctx.stroke();
    }
    /**绘制干扰点**/
    for(var i=0; i<100; i++){
        ctx.fillStyle = randomColor(0,255);
        ctx.beginPath();
        ctx.arc(randomNum(0,width),randomNum(0,height), 1, 0, 2*Math.PI);
        ctx.fill();
    }
}
//登录错误信息提示
function showLoginError(content) {
    $("#loginWarn").text(content).show(function () {
        $("#loginWarn").animate({opacity:0},'slow',function () {
            $("#loginWarn").animate({opacity:1},'slow');
        });
    });
}
//注册错误信息提示
function showRegisterError(content) {
    $("#registerWarn").text(content).show(function () {
        $("#registerWarn").animate({opacity:0},'slow',function () {
            $("#registerWarn").animate({opacity:1},'slow');
        });
    });
}





